package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.Master;
import team.yidong.fenda.entity.User;
import team.yidong.fenda.repository.MasterRepository;
import team.yidong.fenda.repository.UserRepository;
import team.yidong.fenda.service.BlackListService;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuanlong on 2017/11/10.
 */
@Service
public class BlackListServiceImpl implements BlackListService {
    @Autowired
    UserRepository userRepository;
    @Autowired
    MasterRepository masterRepository;

    @Override
    public JSONObject findBlack(Integer hospitalId, String hospitalName, String name, String blackType) {

        //查找user中的黑名单
        JsonConfig jsonConfig = new JsonConfig();

        jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "masterScoreSet", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList","masterAnswerList"});
        List<User> userList = userRepository.findAll(new Specification<User>() {


            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                Join<User, Hospital> join = root.join("hospital");
                if (true) {
                    Predicate p = criteriaBuilder.equal(root.get("isBlack").as(String.class), "是");
                    predicateList.add(p);
                }
                if (hospitalId != 1) {
                    Predicate p = criteriaBuilder.equal(join.get("id").as(Integer.class), hospitalId);
                    predicateList.add(p);
                }
                if (hospitalName != null && !hospitalName.equals("")) {
                    Predicate p = criteriaBuilder.equal(join.get("name").as(String.class), hospitalName);
                    predicateList.add(p);
                }
                if (name != null && !name.equals("")) {
                    Predicate p = criteriaBuilder.like(root.get("name").as(String.class), "%"+name+"%");
                    predicateList.add(p);
                }

                criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
                return null;
            }
        });

        JSONArray jsonArray1 = JSONArray.fromObject(userList, jsonConfig);

        JSONArray ja1 = new JSONArray();

        for (int i = 0; i < jsonArray1.size(); i++) {
            JSONObject jb = new JSONObject();
            jb.put("id", jsonArray1.getJSONObject(i).getString("id"));
            jb.put("kind", "用户");

            jb.put("name", jsonArray1.getJSONObject(i).getString("name"));
            jb.put("hospitalName", jsonArray1.getJSONObject(i).getJSONObject("hospital").getString("name"));
            ja1.element(jb);
        }

        //查找master中的黑名单

        List<Master> masterList = masterRepository.findAll(new Specification<Master>() {
            List<Predicate> predicateList = new ArrayList<>();

            @Override
            public Predicate toPredicate(Root<Master> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                Join<Master, Hospital> join = root.join("hospital");
                if (hospitalId != 1) {
                    Predicate p = criteriaBuilder.equal(join.get("id").as(Integer.class), hospitalId);
                    predicateList.add(p);
                }
                if (true) {
                    Predicate p = criteriaBuilder.equal(root.get("isBlack").as(String.class), "是");
                    predicateList.add(p);
                }
                if (hospitalName != null && !hospitalName.equals("")) {
                    Predicate p = criteriaBuilder.equal(join.get("name").as(String.class), hospitalName);
                    predicateList.add(p);
                }
                if (name != null && !name.equals("")) {
                    Predicate p = criteriaBuilder.like(root.get("name").as(String.class), "%"+name+"%");
                    predicateList.add(p);
                }

                criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
                return null;
            }
        });
        JSONArray jsonArray2 = JSONArray.fromObject(masterList, jsonConfig);

        JSONArray ja2 = new JSONArray();

        for (int i = 0; i < jsonArray2.size(); i++) {
            JSONObject jb = new JSONObject();
            jb.put("id", jsonArray2.getJSONObject(i).getString("id"));
            jb.put("kind", "专家");

            jb.put("name", jsonArray2.getJSONObject(i).getString("name"));
            jb.put("hospitalName", jsonArray2.getJSONObject(i).getJSONObject("hospital").getString("name"));
            ja2.element(jb);
        }

        //合并
        JSONArray jsonArray = new JSONArray();
        if ("用户".equals(blackType)){
            int len = ja1.size();
            for (int i = 0; i < ja1.size(); i++) {
                jsonArray.element(ja1.getJSONObject(i));
            }
            String temp = "{\"total\":" + len + ",\"rows\":" + jsonArray + "}";
            JSONObject jsonObject = JSONObject.fromObject(temp);
            return jsonObject;

        }else if ("专家".equals(blackType)){
            int len = ja2.size();
            for (int j = 0; j < ja2.size(); j++) {
                jsonArray.element(ja2.get(j));
            }
            String temp = "{\"total\":" + len + ",\"rows\":" + jsonArray + "}";
            JSONObject jsonObject = JSONObject.fromObject(temp);
            return jsonObject;
        }else {
            int len = ja1.size() + ja2.size();
            for (int i = 0; i < ja1.size(); i++) {
                jsonArray.element(ja1.getJSONObject(i));
            }
            for (int j = 0; j < ja2.size(); j++) {
                jsonArray.element(ja2.get(j));
            }
            String temp = "{\"total\":" + len + ",\"rows\":" + jsonArray + "}";
            JSONObject jsonObject = JSONObject.fromObject(temp);
            return jsonObject;
        }


    }

    @Transactional
    @Override
    public void removeBlack(Integer id, String kind) {
        if (kind.equals("用户")) {

            User user = userRepository.findOne(id);
            user.setIsBlack("否");
            userRepository.saveAndFlush(user);
        } else if (kind.equals("专家")) {
            Master master = masterRepository.findOne(id);
            master.setIsBlack("否");
            masterRepository.saveAndFlush(master);
        }

    }
}
